package app.modules.cloudpivot.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 接口耗时时间打印
 *
 * @author LiuGangQiang Create in 2024/10/23
 */
//@Aspect
//@Component
public class InterfaceTimeAspect {
    private static final Logger LOG = LoggerFactory.getLogger(InterfaceTimeAspect.class);

    /**
     * 打印耗时时长
     *
     * @author LiuGangQiang Create in 2024/10/23
     * @param point 切点
     * @return {@link Object}
     * @throws Throwable
     */
    @Around("@within(org.springframework.web.bind.annotation.RestController) || @within(org.springframework.stereotype.Controller)")
    public Object exectime(ProceedingJoinPoint point) throws Throwable {
        /* 获取类名 */
        String className = point.getTarget().getClass().getSimpleName();
        /* 获取方法名 */
        String methodName = point.getSignature().getName();
        /* 记录开始时间 */
        long start = System.currentTimeMillis();
        /* 执行业务方法 */
        Object proceed = point.proceed();
        /* 记录结束时间 */
        long executionTime = System.currentTimeMillis() - start;
        /* 打印耗时信息 */
        LOG.warn("===>>执行类[{}]中的方法[{}]耗时：{}ms", className, methodName, executionTime);
        return proceed;
    }
}
